package com.py.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.py.article.service.ApArticleConfigService;
import com.py.common.constants.WmNewsMessageConstants;
import com.py.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author yipeng
 * @create 2023-03-19
 */
@Component
@Slf4j
public class ArticleUpOrDown {
    @Autowired
    private ApArticleConfigService configService;

    /**
     * 监听文章上下架
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message) {
        //转换数据
        if (StringUtils.isNotEmpty(message)) {
            Map map = JSON.parseObject(message, Map.class);
            //修改文章上下架配置
            boolean isDown = true;
            if (map.get("enable").equals(1)) {
                isDown = false;
            }
            configService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown, isDown)
                    .eq(ApArticleConfig::getArticleId, map.get("articleId")));
        }

    }

}
